using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Comteck.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraTreeList.Columns;

namespace Comteck.Winforms {
  /// <summary>
  /// 
  /// </summary>
  public static partial class DevExtensions {
    /// <summary>
    /// 格式化控件显示值
    /// </summary>
    /// <param name="control"></param>
    /// <param name="format"></param>
    public static void FormatDate(this BaseEdit control, EnumDateTimeFormat format = EnumDateTimeFormat.YYYYMMDDHHMMSS) {
      control.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
      control.Properties.EditFormat.FormatString = DateTimeHelper.GetFormatString(format);
      control.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
      control.Properties.DisplayFormat.FormatString = DateTimeHelper.GetFormatString(format);
    }

    /// <summary>
    /// 格式化控件显示值
    /// </summary>
    /// <param name="control"></param>
    /// <param name="format"></param>
    public static void FormatDate(this TreeListColumn control, EnumDateTimeFormat format = EnumDateTimeFormat.YYYYMMDDHHMMSS) {
      control.Format.FormatType = DevExpress.Utils.FormatType.DateTime;
      control.Format.FormatString = DateTimeHelper.GetFormatString(format);
    }

    /// <summary>
    /// 格式化控件显示值
    /// </summary>
    /// <param name="control"></param>
    /// <param name="format"></param>
    public static void FormatDate(this GridColumn control, EnumDateTimeFormat format = EnumDateTimeFormat.YYYYMMDDHHMMSS) {
      control.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
      control.DisplayFormat.FormatString = DateTimeHelper.GetFormatString(format);
    }

    /// <summary>
    /// 格式化控制显示值
    /// </summary>
    /// <param name="control"></param>
    /// <param name="digits"></param>
    public static void FormatAmt(this GridColumn control, int digits) {
      var formatSplit = string.Empty;

      switch (digits) {
        case 1:
          formatSplit = "0.#";
          break;
        case 2:
          formatSplit = "0.##";
          break;
        case 3:
          formatSplit = "0.###";
          break;
        case 4:
          formatSplit = "0.####";
          break;
        case 5:
          formatSplit = "0.#####";
          break;
        case 6:
          formatSplit = "0.######";
          break;
        default:
          formatSplit = "0.######";
          break;
      }

      var format = $"{{0:{formatSplit}}}";

      // dispay format
      control.DisplayFormat.FormatString = format;
      control.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;

      // sub edit
      if (control.ColumnEdit is RepositoryItemSpinEdit spinEdit) {
        spinEdit.DisplayFormat.FormatString = format;
        spinEdit.EditFormat.FormatString = format;

        var index = spinEdit.Mask.EditMask.IndexOf("0.");
        if (index >= 0) {
          spinEdit.Mask.EditMask = $"{spinEdit.Mask.EditMask.Substring(0, index)}{formatSplit}";
        }
      }

      // summary
      if (control.Summary.Count > 0) {
        control.Summary[0].DisplayFormat = format;
      }

    }
  }
}
